﻿Public Class StudentMerger

    Public Sub merge(ByVal toStay As Student, ByVal toDelete As Student)

        'merges this Student model object (toStay) with the "other" instance 
        Dim sdb As New StudentDBMapper
        Dim cmdb As New ClassMemberDBMapper(sdb.dbConn)
        Dim dedb As New DiplomaEarnedDBMapper(sdb.dbConn)
        Dim sledb As New StudentLessonCreditDBMapper(sdb.dbConn)
        Dim cedb As New CertificationEarnedDBMapper(sdb.dbConn)

        Try
            sdb.beginTrans()
            'first, check if id  number are the same
            'note: we use sql to do the merge to avoid any validations of model objects
            Me.mergeStudentInfo(toStay, toDelete)
            sdb.dbConn.executeSQLWithParams("update ClassMember set studentid={0} where studentid={1} ", _
                                toStay.StudentId, toDelete.StudentId)
           

            sdb.dbConn.executeSQLWithParams("update DiplomaEarned set studentid={0} where studentid={1} ", _
                               toStay.StudentId, toDelete.StudentId)

            sdb.dbConn.executeSQLWithParams("update StudentLessonException set studentid={0} where studentid={1} ", _
                                 toStay.StudentId, toDelete.StudentId)

            sdb.dbConn.executeSQLWithParams("update CertificationEarned set studentid={0} where studentid={1} ", _
                                 toStay.StudentId, toDelete.StudentId)

            
            Call sdb.delete(toDelete) 'delete to avoid any possible id number collisions duplicates
            Call sdb.saveStudent(toStay)


            sdb.commitTrans()

        Finally
            sdb.rollbackTrans()
        End Try

    End Sub

    Private Sub mergeStudentInfo(ByVal toStay As Student, ByVal other As Student)

        If Not String.IsNullOrEmpty(other.FirstName) AndAlso _
                     String.IsNullOrEmpty(toStay.FirstName) Then
            toStay.FirstName = other.FirstName
        End If
        If Not String.IsNullOrEmpty(other.LastName) AndAlso _
           String.IsNullOrEmpty(toStay.LastName) Then
            toStay.LastName = other.LastName
        End If
        If Not other.DateOfBirth Is Nothing AndAlso _
           toStay.DateOfBirth Is Nothing Then
            toStay.DateOfBirth = other.DateOfBirth
        End If
        If Not String.IsNullOrEmpty(other.Company) AndAlso _
           String.IsNullOrEmpty(toStay.Company) Then
            toStay.Company = other.Company
        End If
        If Not String.IsNullOrEmpty(other.IdNumber) AndAlso _
           String.IsNullOrEmpty(toStay.IdNumber) Then
            toStay.IdNumber = other.IdNumber
        End If
        If Not String.IsNullOrEmpty(other.HomePhone) AndAlso _
           String.IsNullOrEmpty(toStay.HomePhone) Then
            toStay.HomePhone = other.HomePhone
        End If
        If Not String.IsNullOrEmpty(other.OfficePhone) AndAlso _
           String.IsNullOrEmpty(toStay.OfficePhone) Then
            toStay.OfficePhone = other.OfficePhone
        End If
        If Not String.IsNullOrEmpty(other.MobilePhone) AndAlso _
           String.IsNullOrEmpty(toStay.MobilePhone) Then
            toStay.MobilePhone = other.MobilePhone
        End If
        If Not String.IsNullOrEmpty(other.Fax) AndAlso _
           String.IsNullOrEmpty(toStay.Fax) Then
            toStay.Fax = other.Fax
        End If
        If Not String.IsNullOrEmpty(other.Email) AndAlso _
           String.IsNullOrEmpty(toStay.Email) Then
            toStay.Email = other.Email
        End If
        If Not other.CreateDate Is Nothing AndAlso _
           toStay.CreateDate Is Nothing Then
            toStay.CreateDate = other.CreateDate
        End If
        If Not other.InformCompanyFlag Is Nothing AndAlso _
           toStay.InformCompanyFlag Is Nothing Then
            toStay.InformCompanyFlag = other.InformCompanyFlag
        End If
        If Not String.IsNullOrEmpty(other.Address1) AndAlso _
           String.IsNullOrEmpty(toStay.Address1) Then
            toStay.Address1 = other.Address1
        End If
        If Not String.IsNullOrEmpty(other.Address2) AndAlso _
           String.IsNullOrEmpty(toStay.Address2) Then
            toStay.Address2 = other.Address2
        End If
        If Not String.IsNullOrEmpty(other.Address3) AndAlso _
           String.IsNullOrEmpty(toStay.Address3) Then
            toStay.Address3 = other.Address3
        End If
        If Not String.IsNullOrEmpty(other.Address4) AndAlso _
           String.IsNullOrEmpty(toStay.Address4) Then
            toStay.Address4 = other.Address4
        End If
        If Not other.UpdateDate Is Nothing AndAlso _
           toStay.UpdateDate Is Nothing Then
            toStay.UpdateDate = other.UpdateDate
        End If
        If Not String.IsNullOrEmpty(other.CreateUser) AndAlso _
           String.IsNullOrEmpty(toStay.CreateUser) Then
            toStay.CreateUser = other.CreateUser
        End If
        If Not String.IsNullOrEmpty(other.UpdateUser) AndAlso _
           String.IsNullOrEmpty(toStay.UpdateUser) Then
            toStay.UpdateUser = other.UpdateUser
        End If
        If Not other.IsMember Is Nothing AndAlso _
           toStay.IsMember Is Nothing Then
            toStay.IsMember = other.IsMember
        End If
        If Not other.CountryId Is Nothing AndAlso _
           toStay.CountryId Is Nothing Then
            toStay.CountryId = other.CountryId
        End If
        If Not other.LastPaymentDate Is Nothing AndAlso _
           toStay.LastPaymentDate Is Nothing Then
            toStay.LastPaymentDate = other.LastPaymentDate
        End If
        If Not other.LastSubscriptionYear Is Nothing AndAlso _
           toStay.LastSubscriptionYear Is Nothing Then
            toStay.LastSubscriptionYear = other.LastSubscriptionYear
        End If
        If Not other.MemberSince Is Nothing AndAlso _
           toStay.MemberSince Is Nothing Then
            toStay.MemberSince = other.MemberSince
        End If
        If Not other.IsMemberActive Is Nothing AndAlso _
           toStay.IsMemberActive Is Nothing Then
            toStay.IsMemberActive = other.IsMemberActive
        End If
        If Not other.DistrictId Is Nothing AndAlso _
           toStay.DistrictId Is Nothing Then
            toStay.DistrictId = other.DistrictId
        End If
        If Not String.IsNullOrEmpty(other.DataConversionSourceTable) AndAlso _
           String.IsNullOrEmpty(toStay.DataConversionSourceTable) Then
            toStay.DataConversionSourceTable = other.DataConversionSourceTable
        End If
        If Not other.MemberNumber Is Nothing AndAlso _
           toStay.MemberNumber Is Nothing Then
            toStay.MemberNumber = other.MemberNumber
        End If
    End Sub
End Class
